package com.jeff.springWebTestNG.persistence.service;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Service;

import com.jeff.springWebTestNG.persistence.data.User1;

@Service
public class User1PersisterJdbcImpl extends BaseJdbcPersister implements IUser1Persister {
	
	@Override
	public void create(User1 user) {
		boolean switcher = true;
		
		String sql = "insert into user_1 (name, age) values ('" + user.getName() + "', " + user.getAge() + ")";
		jdbcTemplate.update(sql);
		
		if (switcher) throw new RuntimeException("RollBack");
		
		sql = "insert into user_1 (name, age) values ('Jeff1_Friend', 29)";
		jdbcTemplate.update(sql);
		
		sql = "select * from user_1 limit 1";
		ResultSetExtractor<User1> rst = new ResultSetExtractor<User1>() {
			@Override
			public User1 extractData(ResultSet rs) throws SQLException,
					DataAccessException {
				// TODO Auto-generated method stub
				User1 ret = null;
				if (rs.next()) {
					ret = new User1();
					ret.setId(rs.getInt("id"));
					ret.setName(rs.getString("name"));
					ret.setAge(rs.getInt("age"));
				}
				
				return ret;
			}
		};
		User1 u = jdbcTemplate.query(sql, rst);
		System.out.println(u);
		
	}
	
	@Override
	public User1 getById(int id) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public User1 getByName(String name) {
		// TODO Auto-generated method stub
		return null;
	}
}
